import * as bcrypt from 'bcryptjs'
import dayjs from 'dayjs';
export interface UserConfig {
    hash: number;
    jwt: JwtConfig;
}

/**
 * JWT配置
 */
export interface JwtConfig {
    secret: string;
    token_expired: number;
    refresh_secret: string;
    refresh_token_expired: number;
}

/**
 * JWT荷载对象类型
 */
export interface JwtPayload {
    sub: string;
    iat: number;
}
export const defaultUserConfig = (): UserConfig => {
    return {
        hash: 10,
        jwt: {
            secret: 'dddd',
            token_expired:  3600,
            refresh_secret: 'ddddddd',
            refresh_token_expired:  3600 * 30,
        },
    };
};

export const encrypt = async (password: string) => {
    return bcrypt.hashSync(password,  10);
};

export const decrypt = (password: string, hashed: string) => {
    return bcrypt.compareSync(password, hashed);
};


/**
 * 用户列表查询排序方式
 */
export enum UserOrderType {
    CREATED = 'createdAt',
    UPDATED = 'updatedAt',
}

/**
 * 用户请求DTO验证组
 */
export enum UserValidateGroups {
    CREATE = 'user-create',
    UPDATE = 'user-update',
    REGISTER = 'user-register',
}

export interface JwtPayload {
    sub: string;
    iat: number;
}
export interface TimeOptions {
    /**
     * 时间
     */
    date?: dayjs.ConfigType;
    /**
     * 输出格式
     */
    format?: dayjs.OptionType;
    /**
     * 语言
     */
    locale?: string;
    /**
     * 是否严格模式
     */
    strict?: boolean;
    /**
     * 时区
     */
    zonetime?: string;
}
export type ClassToPlain<T> = { [key in keyof T]: T[key] };
export const ALLOW_GUEST = 'allowGuest';
